package com.pi.iot.tool.service;


import java.util.List;

import com.alibaba.fastjson.JSON;
import com.pi.base.dto.result.ServiceResult;
import com.pi.base.util.lang.IntegerUtil;
import com.pi.iot.repository.huawei.bean.IotPartnerConfig;
import com.pi.iot.repository.huawei.constants.NotifyType;
import com.pi.iot.repository.huawei.service.HuaWeiIotService;

public class PiIotService {
  public static void main(String[] args) throws Exception {
    PiIotService service = new PiIotService();
    String deviceId = "d62389d5-50e7-4575-bebd-53d10505c390";
    String appId = "aGZa0RgUs7f0RZea2BzA7TkWLzAa";
    String appSecret = "no_bMvHqtkxEndF2JfYuRvIB3z4a";
    String verifyCode = "460111173647468";
    String callbackUrl = "http://www.yours.com";
    Integer regCode = Integer.parseInt(IntegerUtil.generateRandomCode(9))+1000000000;
    ServiceResult<Object> result = service.getDeviceInfo(deviceId, appId, appSecret);
    System.out.println(JSON.toJSONString(result.getData()));
    System.out.println(service.deleteDevice(deviceId, appId, appSecret));
    String registerId = service.register(verifyCode , appId, appSecret);
    System.out.println(registerId);
    IotPartnerConfig config = new IotPartnerConfig();
    config.setAppId(appId);
    config.setAppSecret(appSecret);
    config.setConfigVersion(201);
    config.setDeviceType("DoorLock");
    config.setIotDevId(deviceId);
    config.setManufacturerId("Paiai");
    config.setManufacturerName("Paiai");
    config.setModel("LA0101");
    config.setProtocolType("CoAP");
    boolean succeed = service.updateDevice(deviceId, "", config);
    System.out.println("更新设备能力：" + (succeed?"成功":"失败") );
    service.setCert(deviceId, regCode, appId, appSecret);
    service.subscribe(appId, appSecret, NotifyType.DEVICE_DATA_CHANGED, "");
    service.unSubScribe(appId, appSecret, NotifyType.DEVICE_DATA_CHANGED, callbackUrl);
  }
  
  public ServiceResult<String> deleteDevice(
      String deviceId, String appId, String appSecret) throws Exception{
    return HuaWeiIotService.getInstance().deleteDevice(deviceId, appId, appSecret);
  }
  public ServiceResult<Object> getDeviceInfo(
      String deviceId, String appId, String appSecret) throws Exception{
    return HuaWeiIotService.getInstance().getDeviceInfo(deviceId, appId, appSecret);
  }
  
  public void queryGateway(String gateway, String appId, String appSecret) throws Exception{
    HuaWeiIotService.getInstance().queryGateway(gateway, appId, appSecret);
  }
  
  public boolean updateDevice(
      String deviceId, String name, IotPartnerConfig config) throws Exception{
    return HuaWeiIotService.getInstance().updateDevice(deviceId, name, config);
  }
  
  public String setCert(
      String deviceId, Integer regCode,
      String appId, String appSecret) throws Exception{
    return HuaWeiIotService.getInstance().setCert(
        deviceId, regCode, "ge/ad8XT1takmr4STDqV5A==", appId, appSecret);
  }
  
  public String sendUserIcCard( String deviceId, Long userId, String icCard,
      String appId, String appSecret) throws Exception{
    return HuaWeiIotService.getInstance().sendUserIcCard(
        deviceId, userId, icCard, appId, appSecret);
  }
  public String deleteUserIcCard( String deviceId, Long userId, String icCard,
      String appId, String appSecret) throws Exception{
    return HuaWeiIotService.getInstance().deleteUserIcCard(
        deviceId, userId, icCard, appId, appSecret);
  }
  public String deleteAllUserIcCard( String deviceId, Long userId,
      String appId, String appSecret) throws Exception{
    return HuaWeiIotService.getInstance()
        .deleteAllUserIcCard(deviceId, userId, appId, appSecret);
  }

  public String addUserFinger(String deviceId, Long userId,      
      String appId, String appSecret) throws Exception{
    return HuaWeiIotService.getInstance()
        .addUserFinger(deviceId, userId, appId, appSecret);
  }
  
  public String delUserFinger( String deviceId, Long userId, Short fingerId,
      String appId, String appSecret) throws Exception{
    return HuaWeiIotService.getInstance()
        .delUserFinger(deviceId, userId, fingerId, appId, appSecret);
  }
  
  public String delAllUserFinger( String deviceId, Long userId,
      String appId, String appSecret) throws Exception{
    return HuaWeiIotService.getInstance()
        .delAllUserFinger(deviceId, userId, appId, appSecret);
  }
  
  public String addLocalUserPassword( String deviceId, Long userId, 
      String appId, String appSecret) throws Exception{
    return HuaWeiIotService.getInstance()
        .addLocalUserPassword(deviceId, userId, appId, appSecret);
  }
  
 public String addOnlineUserPassword( String deviceId, Long userId, String password,
      String appId, String appSecret) throws Exception{
   return HuaWeiIotService.getInstance()
       .addOnlineUserPassword(deviceId, userId, password, appId, appSecret);
  }
  public String delUserPassword(String deviceId, Long userId,
       Short passwordId,
      String appId, String appSecret) throws Exception{
    return HuaWeiIotService.getInstance()
        .delUserPassword(deviceId, userId, passwordId, appId, appSecret);
  }
  public String delAllUserPassword(String deviceId, Long userId,
      String appId, String appSecret) throws Exception{
    return HuaWeiIotService.getInstance()
        .delAllUserPassword(deviceId, userId, appId, appSecret);
  }
  public String addTempPassword(String deviceId, String tempPassword,
      String appId, String appSecret) throws Exception{
    return HuaWeiIotService.getInstance()
        .addTempPassword(deviceId, tempPassword, appId, appSecret);
  }

  public String openLock(
      String deviceId, String identityCode,
      String appId, String appSecret) throws Exception{
    return HuaWeiIotService.getInstance()
        .openLock(deviceId, identityCode, appId, appSecret);
  }

  public List<String> subscribe(
      String appId, String appSecret, 
      NotifyType notifyType, String subscribeUrl) 
          throws Exception{
    return HuaWeiIotService.getInstance().subscribe(appId, appSecret, notifyType, subscribeUrl);
  }
  
  public void unSubScribe(
      String appId, String appSecret, NotifyType notifyType, String callbackUrl)
          throws Exception{
    HuaWeiIotService.getInstance().unSubScribe(appId, appSecret, notifyType, callbackUrl);
  }
  
  public String register(
      String verifyCode, String appId, String appSecret) 
          throws Exception{
    return HuaWeiIotService.getInstance().register(verifyCode, appId, appSecret);
  }
}
